<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="工种" prop="basicdataCraft"
                :rules="[
                  {required: true, message:'工种不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.basicdataCraft" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('basicdata_craft')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="物料编码" prop="basicdataMaterialCode"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.basicdataMaterialCode" placeholder="请填写物料编码"     ></el-input> -->
              <el-select v-model="inputForm.basicdataMaterialCode" placeholder="请选择"  style="width: 100%;">
                    </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工具名称" prop="basicdataToolName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.basicdataToolName" placeholder="请填写工具名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="规格型号" prop="basicdataMoudle"
                :rules="[
                 ]">
                <el-select v-model="inputForm.basicdataMoudle" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单位" prop="basicdataUnit"
                :rules="[
                 ]">
              <el-input v-model="inputForm.basicdataUnit" placeholder="请填写单位"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="数量" prop="basicdataCount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.basicdataCount" placeholder="请填写数量"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="使用年限" prop="basicdataUsefulLife"
                :rules="[
                 ]">
                  <el-input-number style="width: 80%;" v-model="num" @change="handleChange" :min="1" :max="12" label="描述文字"></el-input-number>
              <el-input style="width: 20%;" disabled v-model="day" ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        num: 1,
        day: '月',
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          basicdataCraft: '',
          basicdataMaterialCode: '',
          basicdataToolName: '',
          basicdataMoudle: '',
          basicdataUnit: '',
          basicdataCount: '',
          basicdataUsefulLife: '',
          remarks: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建工具配备标准`
        } else if (method === 'edit') {
          this.title = '修改工具配备标准'
        } else if (method === 'view') {
          this.title = '查看工具配备标准'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/basicdata/standard/basicdataToolStandard/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.basicdataToolStandard)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.basicdataUsefulLife = this.num + this.day
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/basicdata/standard/basicdataToolStandard/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
